package com.senyint.imsdk.listener;

import org.jivesoftware.smack.ConnectionListener;
import org.jivesoftware.smack.XMPPException;
import org.jivesoftware.smack.packet.StreamError;

import com.senyint.imsdk.ISdkConfig;

public class XMPPConnecionListener implements ConnectionListener {
	private static final String TAG = XMPPConnecionListener.class.getSimpleName();
	
	@Override
	public void connectionClosed() {
		log(TAG,"connectionClosed.");
	}

	@Override
	public void connectionClosedOnError(Exception ex) {
		boolean isConflict = false;
		if(ex.getMessage().contains("conflict")){
			isConflict = true ;
		}
		
		if (ex != null && ex instanceof XMPPException) {
		    XMPPException xmppEx = (XMPPException) ex;
		    StreamError error = xmppEx.getStreamError();
		    String reason = error.getCode();
		    if ("conflict".equals(reason)) {
				isConflict = true ;
		    } else if ("system-shutdown".equals(reason)) {
		    } else {
		    }
		}

		if(isConflict){
			onUserConflict();
		}
		
	}

	@Override
	public void reconnectingIn(int seconds) {
		log(TAG, "reconnectingIn:"+seconds);
	}

	@Override
	public void reconnectionSuccessful() {
		log(TAG, "reconnectionSuccessful");
	}

	@Override
	public void reconnectionFailed(Exception e) {
		log(TAG, "re fail");
	}


	private void log(String tag, String string) {
		System.out.println(tag+"---"+string);
	}

	/**
	 * 登录冲突<br>
	 * 同一账号多设备登录导致冲突
	 */
	public void onUserConflict() {
		log(TAG, "onUserConflict:");
	}

}
